#!/usr/bin/python3
import sys
import requests
import os

# HTTP endpoint for authentication

endport = "#{uri}"
# HTTP endpoint for authentication
AUTH_ENDPOINT = endport+"/client/isok"

def authenticate(username, password):
    try:
        # Make an HTTP request to the authentication endpoint
        response = requests.get(AUTH_ENDPOINT, params={'username':username,'password':password})

        # Check if the response status code is 200
        if dict(response.json()).get("code") == 200:
            print("OK")
            exit(0)  # Authentication succeeded
        else:
            print(dict(response.json()).get("code"))
            exit(1)  # Authentication failed
    except Exception as e:
        print("Error during authentication: {}".format(str(e)))
        exit(1)

if __name__ == "__main__":
    # Read the username and password from OpenVPN
    auth_file = os.environ.get('auth_file')
    username = os.environ.get('username')
    password = os.environ.get('password')
    # Perform custom authentication logic
    authenticate(username, password)
